#region File Description
//-----------------------------------------------------------------------------
// InputState.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion

#region Using Statements
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using System.Collections.Generic;
#endregion

namespace Kristall.Managers
{
    /// <summary>
    /// Helper for reading input from keyboard, gamepad, and touch input. This class 
    /// tracks both the current and previous state of the input devices, and implements 
    /// query methods for high level input actions such as "move up through the menu"
    /// or "pause the game".
    /// </summary>
    public class InputState
    {
        #region Fields

        public KeyboardState CurrentKeyboardStates;
        public KeyboardState LastKeyboardStates;
        private Dictionary<Keys, float> inputTimer = new Dictionary<Keys, float>();

        #endregion

        #region Initialization


        /// <summary>
        /// Constructs a new input state.
        /// </summary>
        public InputState()
        {
            CurrentKeyboardStates = Keyboard.GetState();
            LastKeyboardStates = Keyboard.GetState();
        }


        #endregion

        #region Public Methods


        /// <summary>
        /// Reads the latest state of the keyboard and gamepad.
        /// </summary>
        public void Update()
        {
            LastKeyboardStates = CurrentKeyboardStates;
            CurrentKeyboardStates = Keyboard.GetState();
        }

        public bool IsKeyPressed(Keys key)
        {
            return LastKeyboardStates.IsKeyUp(key) && CurrentKeyboardStates.IsKeyDown(key);
        }

        public bool IsKeyPressed(Keys key, float dt)
        {
            if (CurrentKeyboardStates.IsKeyDown(key) && LastKeyboardStates.IsKeyDown(key))
            {
                if (!inputTimer.ContainsKey(key))
                {
                    inputTimer.Add(key, dt);
                }
                else
                {
                    inputTimer[key] += dt;
                }
            }
            else
            {
                if (!inputTimer.ContainsKey(key))
                {
                    inputTimer.Add(key, 0);
                }
                else
                {
                    inputTimer[key] = 0;
                }
            }

            return LastKeyboardStates.IsKeyUp(key) && CurrentKeyboardStates.IsKeyDown(key) || inputTimer[key] > 0.5f;
        }

        /// <summary>
        /// Helper for checking if a key was newly pressed during this update. The
        /// controllingPlayer parameter specifies which player to read input for.
        /// If this is null, it will accept input from any player. When a keypress
        /// is detected, the output playerIndex reports which player pressed it.
        /// </summary>
        public bool IsNewKeyPress(Keys key)
        {
            return (CurrentKeyboardStates.IsKeyDown(key) && LastKeyboardStates.IsKeyUp(key));
        }


        public bool IsMenuSelect
        {
            get
            {
                return IsNewKeyPress(Keys.Space) || IsNewKeyPress(Keys.Enter);
            }
        }

        public bool IsMenuCancel
        {
            get
            {
                return IsNewKeyPress(Keys.Escape);
            }
        }


        public bool IsMenuUp
        {
            get
            {
                return IsNewKeyPress(Keys.Up) || IsNewKeyPress(Keys.Left) || IsNewKeyPress(Keys.W);
            }
        }


        public bool IsMenuDown
        {
            get
            {
                return IsNewKeyPress(Keys.Down) || IsNewKeyPress(Keys.Right) || IsNewKeyPress(Keys.S);
            }
        }


        public bool IsPauseGame
        {
            get
            {
                return IsNewKeyPress(Keys.Escape);
            }
        }


        #endregion
    }
}
